<?php
/**
 * Created by PhpStorm.
 * 定时脚本类
 * User: 李洋波
 * Date: 2016/9/26
 * Time: 13:27
 */

class timingScript extends CI_Controller{
    function __construct()
    {
        parent::__construct();
        //加载模型
        $this->load->model('Users_model');
        $this->load->model('scheduled_model');
    }

    /**
     * 自动充值
     * 每个月月底 自动还清欠款
     */
    public function AutoRecharge(){
        //充值账户列表
        $rechargeAccountList=array(18,17,12,11,10,9,7,20,16,38);
        //设置时区
        date_default_timezone_set('PRC');
        //获取当前天数
        $day=date('d');
        //获取本月最大天数
        $maxDay=27;
        if($day==$maxDay) {
            $accountData = $this->Users_model->customerslist();
            foreach ($accountData as $row) {
                if (in_array($row['id'], $rechargeAccountList) && $row['Balances'] < 0) {
                    $data = array(
                        'customers_id' => $row['id'],
                        'money' => abs($row['Balances']),
                        'currency' => $row['CurrencyIsoCode'],
                        'transactionId' => '系统自动充值',
                        'time' => time()
                    );
                    $result = $this->Users_model->AddDepositRecord($data);
                    if ($result) {
                        $this->Users_model->UpdateBalances($row['id'], abs($row['Balances']), 'Deposit');
                    }
                }
            }
        }else{
            echo '当前'.$day.'号,本月最大天数'.$maxDay;
        }
    }

    /**
     * 处理异常扣款
     */
    public function disposeChargeError(){
        $data=$this->scheduled_model->getChargeError();
        if($data){
            foreach($data as $row) {
                if ($row['money'] != 0) {
                    $customers = $this->scheduled_model->selectFirst('mt_customers', array('id' => $row['customers_id']));
                    if (strcasecmp($row['order_currency'], $row['customers_currency'])) {
                        $payMoney = currency_service($row['order_currency'], $row['customers_currency'], $row['money']);
                    } else {
                        $payMoney = $row['money'];
                    }
                    if($payMoney > 0) {
                        //判断余额是否透支完毕
                        $facilityExtent = $customers['facilityExtent'] + ($customers['Balances'] - $payMoney);
                        if ($facilityExtent < 0) {
                            //hold住订单
                            $this->scheduled_model->update('mt_order', array('available' => 2), array('id' => $row['oid']));
                            //更新标识
                            $this->scheduled_model->update('mt_charge_error', array('is_error' => 1), array('id' => $row['id']));
                        } else {
                            //新增扣款记录
                            $add_result = $this->scheduled_model->insert('mt_deposit_record',
                                array(
                                    'customers_id' => $row['customers_id'], 'orderId' => $row['order_num'], 'time' => $row['time'],
                                    'money' => '-' . $payMoney, 'currency' => $row['customers_currency'], 'country_code' => strtolower($row['point'])
                                )
                            );
                            if ($add_result) {
                                //扣款余额
                                $payData['Balances'] = 'Balances-' . $payMoney;
                                $upd_result = $this->scheduled_model->updateset('mt_customers', $payData, array('id' => $row['customers_id']));
                                if ($upd_result) {
                                    //更新订单信息
                                    $setOrderData = array(
                                        'is_pay' => 1, 'payment_id' => '111', 'payment_date' => $row['time'],
                                        'error_msg' => 'pay success!', 'isSyncSap' => '1', 'amount_paid' => $payMoney
                                    );
                                    $this->scheduled_model->update('mt_order', $setOrderData, array('id' => $row['oid']));
                                }
                                //更新标识
                                $this->scheduled_model->update('mt_charge_error', array('is_error' => 1), array('id' => $row['id']));
                            }
                        }
                    }
                }
            }
        }else{
            echo '没有数据需要处理';
        }
    }
}